from rest_framework.viewsets import ViewSet
from book.models import BookInfo
from book.modelserializer import BookModelSerialzier
from rest_framework.response import Response


class BooksView(ViewSet):

    # 如果继承视图集，定义方法时，不必按照请求方式定义
    def list(self, request):
        """
            获取多个图书对象
        :param request:
        :return:
        """
        books = BookInfo.objects.all()
        ser = BookModelSerialzier(books, many=True)
        return Response(ser.data)

    # 获取最后一本图书
    def last_book(self, request):
        book = BookInfo.objects.last()
        ser = BookModelSerialzier(book)
        data = ser.data
        return Response(data)
